package com.hoshiicloud.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.rbac.entity.SysUsers;
import com.hoshiicloud.rbac.vo.SysUsersVO;
import com.hoshiicloud.user.param.rbac.SysUsersQueryParam;
import com.hoshiicloud.user.service.ISaasManageService;
import com.hoshiicloud.user.service.rbac.ISysRolesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * SAAS系统管理后台聚合服务
 * @author chenjie
 * @date 2019/7/30-10:51
 * @email rogercj1@126.com
 */

@Slf4j
@RestController
@RequestMapping("/saasManage")
@Api(value = "SAAS系统管理后台", tags = {"SAAS系统管理后台"})

public class SaasManageController {

    @Autowired
    private ISaasManageService iSaasManageService;


    @ApiOperation("SAAS系统分页查询员工列表")
    @RequestMapping(value = "/getEmployeePage", method = RequestMethod.POST)
    public Response<IPage<SysUsersVO>> getEmployeePage(@Valid @RequestBody SysUsersQueryParam param) {
        //QueryWrapper<SysUsers> queryWrapper = param.convertToQueryWrapper();
        IPage<SysUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
        //String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
        return iSaasManageService.getEmployeePage(page, param,param.getId());
    }

    @ApiOperation("SAAS系统根据部门ID分页查询(包括子部门)员工列表")
    @RequestMapping(value = "/getEmployeePageByDeptId", method = RequestMethod.POST)
    public Response<IPage<SysUsersVO>> getEmployeePageByDeptId(@Valid @RequestBody SysUsersQueryParam param) {
        IPage<SysUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
        String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
        return iSaasManageService.getEmployeePageByDeptId(page, key,param.getDeptId());
    }



    @ApiOperation("SAAS系统查询角色列表")
    @RequestMapping(value = "/getRoleList", method = RequestMethod.POST)
    public Response<IPage<SysUsersVO>> getRoleList(@Valid @RequestBody SysUsersQueryParam param) {
        IPage<SysUsers> page = new Page<>(Long.valueOf(param.getPageNumber()), Long.valueOf(param.getPageSize()));
        String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
        return iSaasManageService.getRoleList(page,key);
    }

    @ApiOperation("SAAS系统根据角色名称分页查询员工列表")
    @RequestMapping(value = "/getEmployeePageByRoleName", method = RequestMethod.POST)
    public Response<IPage<SysUsersVO>> getEmployeePageByRoleName(@Valid @RequestBody SysUsersQueryParam param) {
        IPage<SysUsers> page
                = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
        String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
        return iSaasManageService.getEmployeePageByRoleName(page, key,param.getRoleName());
    }

}
